﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public partial class TLS02
    {


        public TSLS02 SKD02
        {
            get
            {
                return (TSLS02)this.FindName("cSLS02");
            }
        }
        public TLS02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VLIEFDAT();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            base.Window_Loaded(sender, e);
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            cvMaster.CustomFilter = null;
            dwData = TBMain.NewRow();
            dwData["LSMANDANT"] = app.Mandant;
            dwData["LSNAME1"] =ART.Trim();
            SResult = TMV.QueryLIEFDAT(dwData, 8);
            MergeData(SResult, true);
            cvMaster.CustomFilter = null;
        }
        public override void LOADBYAUF()
        {

            if (!IsSaved)
                return;
            if (ORDERNUM.Length < 8)
                return;
            if (ORDERNUM != KEY)
                return;
            dwData = TBMain.NewRow();
            dwData["LSMANDANT"] = app.Mandant;
            dwData["LSLIEFNR"] = " AND LSLIEFNR='" + ORDERNUM.Substring(0, 8) + "'";
            SResult = ((VLIEFDAT)MV).QueryLIEFDAT(dwData, 8);
            TBTemp = TBMain.Clone();
            TBTemp.Merge(SResult.Tables[0]);
            foreach (DataRow row in TBTemp.Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }
        }
        public override void OnEXAUFChg(string vlaue)
        {
            if(EXAUF.Length==0)
            {
                cvMaster.CustomFilter = null;
                return;
            }
            cvMaster.CustomFilter = "LSLIEFNR='" + EXAUF + "'";
            base.OnEXAUFChg(vlaue);
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            TMV.NEWLS(PM);
            vwMaster.View.MoveCurrentToFirst();
            base.NewCommand(sender, e);
        }
        
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            SResult = ((VLIEFDAT)MV).QueryLIEFDAT(dwMaster, -2);
            if (SResult.Tables[0].Rows.Count > 0)
            {
                MessageBox.Show("在" + SResult.Tables[0].TableName+"存在记录,禁止删除");
                return;
            }
          
            ((VLIEFDAT)MV).DeleteLIEFDAT(dwMaster, 1);
            filter = "LSLIEFNR='" + dwMaster["LSLIEFNR"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }
    }
}
